﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Store_Keeper_SK_GTNRequest : System.Web.UI.Page
{

    DataTable cartproduct = new DataTable();
    public static int indexx;
    public static Boolean result;
    public static Boolean result1;
    protected void Page_Load(object sender, EventArgs e)
    {
        ASPxRadioButton1.Visible = false;
        ASPxRadioButton2.Visible = false;
        Session["tot"] = 0;
        Session["current"] = "Good Transfer Note";//current page
        if (Session["user"] != null)//check user
        {
            string user = Session["user"].ToString();
            if (user == "viewer")
            {
                Response.Redirect("index.aspx");
            }

        }
        else
        {
            Response.Redirect("index.aspx");
        }
        cartproduct = new DataTable();

        if (IsPostBack)
        {
            SetInitialRow();
        }
        else
        {
            Session["cart"] = null;
            Session["cartview"] = null;
        }
        if ((Session["cartview"] == null) && (Session["cart"] == null))
        {
            BindGridView();
        }
        if (Session["cartview"] != null)
        {
            cartproduct = (DataTable)Session["cartview"];
        }

    }

    private void SetInitialRow()
    {
        try
        {
            cartproduct.Columns.Add("Item ID");
            cartproduct.Columns.Add("Item Code");

            cartproduct.Columns.Add("Item");
            cartproduct.Columns.Add("Stock Code");


            cartproduct.Columns.Add("cost");
            cartproduct.Columns.Add("Brand");
            cartproduct.Columns.Add("Selling Price");

            cartproduct.Columns.Add("Quantity");


            // Set Produt_ID n Product_code column as the primary key.
            //DataColumn[] dcKey = new DataColumn[1];
            DataColumn[] dcKey = new DataColumn[2];
            dcKey[0] = cartproduct.Columns["Item ID"];
            dcKey[1] = cartproduct.Columns["Item Code"];
            cartproduct.PrimaryKey = dcKey;
        }
        catch
        {
            Response.Redirect("404.ASPX");
        }
    }//initial table



    protected void ASPxGridView1_SelectionChanged(object sender, EventArgs e)
    {

    }

    protected void ASPxButton1_Click(object sender, EventArgs e)
    {
        try
        {
            ASPxRadioButton1.Visible = true;
            ASPxRadioButton2.Visible = true;
            Boolean correct = false;
            Checkout.Visible = true;

            List<object> items = new List<object>();
            List<object> code = new List<object>();
            //   ASPxGridView1.GetSelectedFieldValues("Pro_ID", "Code", "Pro_Price", "Pro_Name", "Business_Value", "Point_Value");

            items.Clear();
            code.Clear();

            items = ASPxGridView1.GetSelectedFieldValues("Pro_ID");
            code = ASPxGridView1.GetSelectedFieldValues("Code");

            if (items.Count == 0 && code.Count == 0)
            {
                error.Visible = true;
                error.Text = "Please Select an Item";
                Checkout.Visible = false;
            }
            else
            {
                int hh = items.Count;
                Checkout.Enabled = true;
                error.Visible = false;
                foreach (object it in items)
                {
                    Label1.Text = it.ToString();
                }


                //Code

                foreach (object cd in code)
                {
                    Label7.Text = cd.ToString();
                }

                int rowc = cartproduct.Rows.Count;
                DataView dv1 = new DataView(cartproduct);

                string Quntity = "10";


                int i = cartproduct.Columns.Count;
                for (int k = 0; k < hh; k++)
                {
                    Bestlife.ERP.Entities.product Re_Product = Bestlife.ERP.DataAccess.D_Issue_Ap.GetProuct(items[k].ToString(), code[k].ToString());

                    string stockid = Bestlife.ERP.DataAccess.D_Issue_Ap.stockid(items[k].ToString(), code[k].ToString());
                    decimal qty = Bestlife.ERP.DataAccess.D_Issue_Ap.ReturnStockQuantity(items[k].ToString(), code[k].ToString());

                    if (Session["cart"] != null)
                    {
                        foreach (DataRowView drv in dv1)
                        {
                            if (drv.Row["Stock Code"].ToString() == stockid)
                            {
                                correct = false;

                            }
                            else
                            {
                                correct = true;
                            }
                        }
                        if (correct)
                        {

                            cartproduct.Rows.Add(Re_Product.Pro_ID, code[k].ToString(), Re_Product.Pro_Name, stockid, Re_Product.Pur_Price, Re_Product.Brand, Re_Product.Ret_Price, Quntity);

                            //for (int g = 0; g < hh; g++)
                            //{
                            //    if (GridView1.SelectedIndex == g)

                            //        GridView1.SelectedIndex =-1;

                            //}
                        }

                    }
                    else
                    {
                        cartproduct.Rows.Add(Re_Product.Pro_ID, code[k].ToString(), Re_Product.Pro_Name, stockid, Re_Product.Pur_Price, Re_Product.Brand, Re_Product.Ret_Price, Quntity);
                    }


                }
                Session["cartview"] = cartproduct;
                GridView1.DataSource = Session["cartview"];
                GridView1.DataBind();

                //lablesvalue();
            }

        }



        catch (Exception ee)
        {
            error.Visible = true;
            error.Text = "This Item has already Added,Please Select Another Item!!";
        }
    }

    protected void Checkout_Click(object sender, EventArgs e)
    {
        string msg;
        //Bestlife.ERP.BusinessRules.Shopping_Cart cartsession = new Bestlife.ERP.BusinessRules.Shopping_Cart();
        Bestlife.ERP.Entities.E_Issue_Ap pro = new Bestlife.ERP.Entities.E_Issue_Ap();
        Session["cart"] = pro;
        string user = Session["user"].ToString();
        pro.Issu_ID1 = Bestlife.ERP.BusinessRules.B_Issue_App.lastissuid().ToString();

        if (ASPxRadioButton1.Checked)
        {
            pro.Branch = "Panadura";
            msg = "Stock Transfed To Panadura Sucessfully!! \n Stock Transfer ID is " + pro.Issu_ID1;
        }
        else
        {
            pro.Branch = "Kandy";
            msg = "Stock Transfed To Kandy Sucessfully!! \n Stock Transfer ID is " + pro.Issu_ID1;
        }

        DataView dv1 = new DataView(cartproduct);
        try
        {

            if (Session["cart"] != null)
            {
                foreach (DataRowView drv in dv1)
                {
                    pro.Pro_id = drv.Row["Item ID"].ToString();
                    pro.Code = drv.Row["Item Code"].ToString();
                    pro.Pro_name1 = drv.Row["Item"].ToString();
                    pro.Stor_ID = drv.Row["Stock Code"].ToString();
                    pro.Cost = Convert.ToDecimal(drv.Row["cost"].ToString());
                    pro.Brand = drv.Row["Brand"].ToString();
                    pro.Ret_price = Convert.ToDecimal(drv.Row["Selling Price"].ToString());
                    pro.Qty = Convert.ToDecimal(drv.Row["Quantity"].ToString());
                    result = Bestlife.ERP.BusinessRules.B_Issue_App.ins_issue(pro, user);
                    decimal total = Bestlife.ERP.BusinessRules.B_PurchaseOrder.linetot(pro.Qty, pro.Cost);
                    pro.Tot = Convert.ToDecimal(Session["tot"].ToString()) + total;
                    Session["tot"] = pro.Tot;

                }
                result1 = Bestlife.ERP.BusinessRules.B_Issue_App.ins_ISSU(pro, user);
                if (result == true && result1 == true)
                {
                    ASPxLabel2.Text = msg;
                    ASPxLabel2.Visible = true;
                    Checkout.Enabled = false;
                    ASPxButton1.Enabled = false;
                    Session["cartview"] = null;
                    GridView1.Enabled = false;
                }
            }
        }
        catch (Exception ee)
        {
            //string msg = ee.ToString();
            //string[] arr = msg.Split('.');
            //string message1 = arr[1].ToString();
            //Response.Redirect("404.aspx?msg='" + message1 + "'");
        }
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            ASPxRadioButton1.Visible = true;
            ASPxRadioButton2.Visible = true;
            Checkout.Enabled = true;


            if (Session["cartview"] != null)
            {
                string tbs = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;

                DataTable bacEdit = (DataTable)Session["cartview"];
                // Get the PersonID of the selected row.
                string Pro_ID = GridView1.Rows[e.RowIndex].Cells[0].Text;
                string Pro_code = GridView1.Rows[e.RowIndex].Cells[1].Text;
                string codex = GridView1.Rows[e.RowIndex].Cells[6].Text;
                DataRow bacEditrw = bacEdit.Rows.Find(new Object[] { Pro_ID, Pro_code });


                Bestlife.ERP.Entities.product Re_Product = Bestlife.ERP.DataAccess.D_Issue_Ap.GetProuct(Pro_ID, Pro_code);


                decimal DBqn = Bestlife.ERP.DataAccess.D_Issue_Ap.ReturnStockQuantity(Pro_ID, Pro_code);
                string tt = bacEditrw[7].ToString();
                decimal quan = Convert.ToDecimal(tt);
                Boolean st = false;
                if (DBqn >= quan)
                {
                    st = true;
                }
                if (st == true)
                {
                    //string Pro_Code = GridView1.Rows[e.RowIndex].Cells[2].Text;

                    // Find the row in DateTable.


                    string tbl = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;

                    bacEditrw["Quantity"] = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;

                    string tb = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;
                    GridView1.EditIndex = -1;
                    BindGridView();

                    #region Ontime Calculation
                    foreach (GridViewRow rws in GridView1.Rows)
                    {
                        string proID = rws.Cells[0].Text;
                        codex = rws.Cells[1].Text;
                        Label _txt = rws.FindControl("Label3") as Label;
                        string Quantity = _txt.Text;

                        if (Quantity.Equals(""))
                        {
                            Quantity = "1";
                        }

                    }

                    #endregion


                }
                else
                {
                    bacEditrw["Quantity"] = "1";
                    // Exit edit mode.
                    GridView1.EditIndex = -1;
                    // Rebind the GridView control to show data after updating.
                    BindGridView();
                    error.Text = "Available Quantity is " + st;
                }
                // Rebind the GridView control to show data after updating.
                BindGridView();
            }
        }
        catch (Exception ee)
        {
            //string msg = ee.ToString();
            //string[] arr = msg.Split('.');
            //string message1 = arr[1].ToString();
            //Response.Redirect("404.aspx?msg='" + message1 + "'");
        }
    }

    private void BindGridView()
    {
        try
        {
            if (Session["cartview"] != null)
            {

                // Get the DataTable from ViewState.
                DataTable cartAdd = (DataTable)Session["cartview"];

                // Co nvert the DataTable to DataView.
                DataView cartvb = new DataView(cartAdd);

                // Bind the GridView control.
                GridView1.DataSource = cartvb;
                GridView1.DataBind();

            }
        }
        catch (Exception ee)
        {
            string msg = ee.ToString();
            string[] arr = msg.Split('.');
            string message1 = arr[1].ToString();
            Response.Redirect("404.aspx?msg='" + message1 + "'");
        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        ASPxRadioButton1.Visible = true;
        ASPxRadioButton2.Visible = true;
        Checkout.Enabled = true;
        GridView1.EditIndex = e.NewEditIndex;
        BindGridView();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            ASPxRadioButton1.Visible = true;
            ASPxRadioButton2.Visible = true;
            Checkout.Enabled = true;
            if (Session["cartview"] != null)
            {
                // Get the DataTable from ViewState.
                DataTable delcart = (DataTable)Session["cartview"];

                // Get the PersonID of the selected row.
                string Pro_ID = GridView1.Rows[e.RowIndex].Cells[0].Text;
                string Pro_code = GridView1.Rows[e.RowIndex].Cells[1].Text;
                //string codex = GridView1.Rows[e.RowIndex].Cells[6].Text;
                DataRow bacDeltrw = delcart.Rows.Find(new Object[] { Pro_ID, Pro_code });


                // Remove the row from the DataTable.
                delcart.Rows.Remove(bacDeltrw);

                // Rebind the GridView control to show data after deleting.
                BindGridView();



            }
        }
        catch (Exception ee)
        {
            //string msg = ee.ToString();
            //string[] arr = msg.Split('.');
            //string message1 = arr[1].ToString();
            //Response.Redirect("404.aspx?msg='" + message1 + "'");
        }
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        ASPxRadioButton1.Visible = true;
        ASPxRadioButton2.Visible = true;
        Checkout.Enabled = true;
        GridView1.EditIndex = -1;
        BindGridView();
    }


}


